﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TodoApp.GoodsModel;
using TodoApp.LiveShopAddressDTO;
using TodoApp.LiveShopGoodsDTO;
using TodoApp.LiveShopUsersDTO;
using TodoApp.OrdesModel;
using TodoApp.TodoGoodsDTO;
using TodoApp.TodoOrdesDTO;
using TodoApp.TodoStatusDTO;
using TodoApp.UsersModel;
using Volo.Abp.Domain.Repositories;

namespace TodoApp
{

 

    /// <summary>
   ///  订单 
    /// </summary>
    public class GoodsOrdesService : Volo.Abp.Application.Services.ApplicationService  , IGoodsOrdesService
    {

        /// <summary>
        ///  注入 订单  
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        private readonly IRepository<TodoGoodsOrdes, int> _todoOrdes; 
        /// <summary>
        /// 注入商品 
        /// </summary>
        private readonly IRepository<LiveShopGoods, int> _liveShopGoods;

        /// <summary>
        ///  注入账号
        /// </summary>

        private readonly IRepository<LiveShopUsers, int> _liveShopUsers;
        /// <summary>
        ///  收货地址 
        /// </summary>

        private readonly IRepository<LiveShopAddress, int> _liveShopAddresses;

        /// <summary>
        ///  订单状态
        /// </summary>

        private readonly IRepository<TodoStatus, int> _todoStatuses;

        public GoodsOrdesService(IRepository<TodoGoodsOrdes, int> todoOrdes ,IRepository<LiveShopGoods, int> db,
            IRepository<LiveShopUsers, int> _db , IRepository<LiveShopAddress, int> _Str ,
            IRepository<TodoStatus, int> todoStatuses
            )
        {
            _todoOrdes = todoOrdes;
            _liveShopGoods = db;
            _liveShopUsers = _db;
            _liveShopAddresses = _Str;
            _todoStatuses = todoStatuses;
        }


        /// <summary>
        ///  删除 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Task DeleteAsync(int id)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        ///  显示 普通订单信息 
        /// </summary>
        /// <returns></returns>
        public async Task<List<TodoGoodsOrdesShowDTO>> GetLIst()
        {
            List<TodoGoodsOrdesShowDTO> str = ObjectMapper.Map<List<TodoGoodsOrdes>, List<TodoGoodsOrdesShowDTO>>(await _todoOrdes.GetListAsync());
        
            return str;
        }
        /// <summary>
        ///  显示  订单 详情 多表联查
        /// </summary>s
        /// <returns></returns>
       public async Task<List<TodoOrdesGoodsDTo>> GetLIstShow()
        {
            //  订单
            List<TodoGoodsOrdesShowDTO> Ordesstr = ObjectMapper.Map<List<TodoGoodsOrdes>, List<TodoGoodsOrdesShowDTO>>(await _todoOrdes.GetListAsync());
            // 收货地址 
            List<LiveShopAddressShowDTO> Addressstr = ObjectMapper.Map<List<LiveShopAddress>, List<LiveShopAddressShowDTO>>(await _liveShopAddresses.GetListAsync());
            //// 商品 
            List<LiveShopGoodsShowDTO> Goodsstr = ObjectMapper.Map<List<LiveShopGoods>, List<LiveShopGoodsShowDTO>>(await _liveShopGoods.GetListAsync());
            //// 订单状态 
            List<TodoStatusShowDTO> Statusstr = ObjectMapper.Map<List<TodoStatus>, List<TodoStatusShowDTO>>(await _todoStatuses.GetListAsync());
            //// 用户 
            List<LiveShopUsersShowDTO> Usersstr = ObjectMapper.Map<List<LiveShopUsers>, List<LiveShopUsersShowDTO>>(await _liveShopUsers.GetListAsync());

            // TodoOrdesGoodsDTo str = new TodoOrdesGoodsDTo();

            var str = (from O in Ordesstr
                       join A in Addressstr on O.LiveShopAddressID equals A.Id
                       join G in Goodsstr on O.LiveShopGoodsID equals G.Id
                       join S in Statusstr on O.TodostatusID equals S.Id
                       join U in Usersstr on O.LiveShopUsersID equals U.Id
                       select

                       new TodoOrdesGoodsDTo()
                       {

                           // 获取 订单 
                           Id = O.Id,
                           OrdersNumber = O.OrdersNumber,
                           OrdersSum = O.OrdersSum,
                           OrdersDateTime = O.OrdersDateTime,
                           TodostatusID = O.TodostatusID,
                           LiveShopUsersID = O.LiveShopUsersID,
                           LiveShopAddressID = O.LiveShopAddressID,
                           LiveShopGoodsID = O.LiveShopGoodsID,
                           // 获取 状态 
                           StaName = S.StaName,
                           // 获取 账号 
                           AdminName = U.Names,
                           State = U.State,
                           // 地址
                           Address = A.Address,
                           Doorplate = A.Doorplate,

                           // 商品 
                           Encode = G.Encode,
                           Names = G.Names,
                           Brief = G.Brief,
                           Img = G.Img,
                           SalePrice = G.CostPrice,
                           CategoryId = G.CategoryId,
                           BrandId = G.BrandId,
                           BuyCount = G.BuyCount,
                       }).ToList();

            return str;

            }

            /// <summary>
            ///  添加 订单 
            /// </summary>
            /// <param name="model"></param>
            /// <returns></returns>
            public async Task<string> InsertAsync(TodoGoodsOrdesAddDTO model)
        {
            await _todoOrdes.InsertAsync(ObjectMapper.Map<TodoGoodsOrdesAddDTO, TodoGoodsOrdes>(model));
            return "添加成功";
        }

       
    }
}
